package com.youlu.campus.service.activity.impl;

import com.youlu.campus.entity.ActivityVideoLike;
import com.youlu.campus.service.activity.ActivityVideoLikeService;
import com.youlu.campus.service.activity.ActivityVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.connection.RedisClusterCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ActivityVideoServiceImpl implements ActivityVideoService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ActivityVideoLikeService activityVideoLikeService;
    // Redis Key 前缀
    private String ACTIVITY_MEDIA_LIKE_KEY_PREFIX = "com.activity.media.like:aId:%s.mid:%s";
    private final String ORIGINAL_USER = "20250512";

    @Override
    public Boolean isDianZan(String userId, String acitivityId, String mId) {

        ActivityVideoLike a = activityVideoLikeService.findBy(userId, acitivityId, mId);
        if (Objects.isNull(a)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
//        String key = this.getLikeKey(acitivityId, mId);
//        log.info(":>>> 是否点赞key:{},activityId:{},userId:{}.mid:{}", key,
//                acitivityId, userId, mId);
//        return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, userId));
    }

    @Override
    public Boolean dianZan(String userId, String acitivityId, String mId) {
        Boolean todo = this.isDianZan(userId, acitivityId, mId);
        if (Boolean.TRUE.equals(todo)) {
            return Boolean.TRUE;
        }
        ActivityVideoLike a = new ActivityVideoLike();
        a.setUserId(userId);
        a.setActivityId(acitivityId);
        a.setMid(mId);
        return activityVideoLikeService.insert(a);
//        String key = this.getLikeKey(acitivityId, mId);
//        Long currentTime = System.currentTimeMillis();
//        Boolean r0 = redisTemplate.opsForZSet().addIfAbsent(key, ORIGINAL_USER, Double.valueOf(currentTime + ""));
//        if (Boolean.TRUE.equals(r0)) {
//            // 设置90天过期
//            redisTemplate.expire(key, 90, TimeUnit.DAYS);
//        }
//        redisTemplate.delete(key);
//        Boolean r = redisTemplate.opsForZSet().addIfAbsent(key, userId, Double.valueOf(currentTime + ""));
//        return r;
    }

    @Override
    public Boolean cancelDianZan(String userId, String acitivityId, String mId) {
        ActivityVideoLike a = activityVideoLikeService.findBy(userId, acitivityId, mId);
        if (Objects.isNull(a)) {
            return Boolean.TRUE;
        }
        return activityVideoLikeService.delete(a.getId());
        //        String key = this.getLikeKey(acitivityId, mId);
//        Long r = redisTemplate.opsForZSet().remove(key, userId);
//        return r == null ? false : false;
    }

    @Override
    public Long getDianZanNum(String acitivityId, String mId) {
        return activityVideoLikeService.getCount(acitivityId, mId);
//        String key = this.getLikeKey(acitivityId, mId);
//        redisTemplate.opsForZSet().size(key);
//        return redisTemplate.opsForZSet().size(key);
    }

    @Override
    public void deleteKey(String acitivityId, String mId) {
        String key = this.getLikeKey(acitivityId, mId);
        redisTemplate.delete(key);
    }

    /**
     * 获取点赞Key前缀
     *
     * @param acitivityId
     * @param mId
     * @return
     */
    private String getLikeKey(String acitivityId, String mId) {
        return String.format(ACTIVITY_MEDIA_LIKE_KEY_PREFIX, acitivityId, mId);
    }
}
